package model;

import java.util.Comparator;
import java.util.Date;

public class App extends ModelBase
{
	private String name;
	private String category;
	private double price;
	private Date releaseDate;
	

	public App()
	{
	}
	
	public App(String name)
	{
		setName(name);
	}
	
	public App(String name, String category, double price, Date relaseDate)
	{
		this(name);
		setCategory(category);
		setPrice(price);
		setReleaseDate(relaseDate);
	}
	
	public App(String name, String category, double price, Date releaseDate, Long id)
	{
		this(name, category, price, releaseDate);
		setID(id);
	}
	
	
	
	public void setName(String name) 
	{
		if(name == null || name.trim().isEmpty()) throw new IllegalArgumentException("App name must not be null");		
		this.name = name;
	}
	
	public String getName() 
	{
		return name;
	}
	
	public void setCategory(String category) 
	{
		if(category == null || name.trim().isEmpty()) throw new IllegalArgumentException("Category must not be null");
		this.category = category;
	}
	
	public String getCategory() 
	{
		return category;
	}
	
	public void setPrice(double price) 
	{
		if(price < 0) throw new IllegalArgumentException("Price must be a positive value");
		this.price = price;
	}
	
	public double getPrice() 
	{
		return price;
	}
	
	public void setReleaseDate(Date releaseDate) 
	{
		if(releaseDate == null) throw new IllegalArgumentException("ReleaseDate must not be null");
		this.releaseDate = (Date) releaseDate.clone();
	}
	
	public Date getReleaseDate() 
	{
		return (Date) releaseDate.clone();
	}
	
	public static class AppByNameComparator implements Comparator<App>
	{
		public int compare(App lhs, App rhs)
		{
			if (lhs.getName().toLowerCase().compareTo(rhs.getName().toLowerCase()) < 0) return -1;
			if (lhs.getName().compareTo(rhs.getName()) > 0) return 1;
			return 0;
		}
	}

	public static class AppByPriceComparator implements Comparator<App>
	{

		public int compare(App lhs, App rhs)
		{
			if (lhs.getPrice() < rhs.getPrice()) return -1;
			if (lhs.getPrice() > rhs.getPrice()) return 1;
			return 0;
		}
	}
	
	public static class AppByCategoryComparator implements Comparator<App>
	{
		public int compare(App lhs, App rhs)
		{
			if (lhs.getCategory().compareTo(rhs.getCategory()) < 0) return -1;
			if (lhs.getCategory().compareTo(rhs.getCategory()) > 0) return 1;
			return 0;
		}
	}



}
